package com.wangdong.mijiqiqi.threetype;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;

import com.wangdong.mijiqiqi.BaseKeyConstants;
import com.wangdong.mijiqiqi.R;


public class ActionBarFragment extends Fragment {
	
	private TextView titleName;
	private TextView leftView;
	private RadioButton rb_right;
	private View titleLayout;
	
	private IActionBarLeftOnClickListener leftOnClickListener;
	private IActionBarRightBtnOnClickListener rightBtnOnClickListener;
	private IActionBarTitleBtnOnClickListener titleBtnOnClickListener;
	
	public static ActionBarFragment newInstance(int titleId, int leftBtnImage, int rightBtnImage) { 
		return newInstance(titleId, -1, leftBtnImage, -1, rightBtnImage);
	}
	
	public static ActionBarFragment newInstance(int titleId, int leftBtnName, int leftBtnImage,
			int rightBtnName, int rightBtnImage) { 
		final ActionBarFragment fragment = new ActionBarFragment();
		final Bundle args = new Bundle();
		args.putInt(BaseKeyConstants.KEY_TITLE, titleId);
		args.putInt(BaseKeyConstants.KEY_LEFTBTNNAME, leftBtnName);
		args.putInt(BaseKeyConstants.KEY_LEFTBTNIMAGE, leftBtnImage);
		args.putInt(BaseKeyConstants.KEY_RIGHTBTNNAME, rightBtnName);
		args.putInt(BaseKeyConstants.KEY_RIGHTBTNIMAGE, rightBtnImage);
		fragment.setArguments(args);
		return fragment;
	}
	   	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	@Override
	public void onResume() {
		super.onResume();
	}
	
	@Override
	public void onPause() {
		super.onPause();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
		
		final View mainView = inflater.inflate(R.layout.action_bar_wdtitle, container, false);
		titleLayout = mainView.findViewById(R.id.title_layout);
		
		leftView = (TextView) mainView.findViewById(R.id.tv_left);
		leftView.setOnClickListener(new ImageView.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				if (leftOnClickListener != null) {
					leftOnClickListener.onClick(arg0);
				}
			}
		});
		
		titleName = (TextView) mainView.findViewById(R.id.tv_title);
		titleName.setOnClickListener(new TextView.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (titleBtnOnClickListener != null) {
					titleBtnOnClickListener.onClick(v);
				}
			}
		});
		
		rb_right = (RadioButton) mainView.findViewById(R.id.rb_right);
		rb_right.setOnClickListener(new TextView.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (rightBtnOnClickListener != null) {
					rightBtnOnClickListener.onClick(v);
				}
			}
		});
		
		{
			final Bundle bundle = getArguments();
			if (bundle != null) {
				final int titleId = bundle.getInt(BaseKeyConstants.KEY_TITLE, -1);
				if (titleId > -1) {
					titleName.setText(titleId);
				}
				
				final int leftBtnNameId = bundle.getInt(BaseKeyConstants.KEY_LEFTBTNNAME, -1);
				if (leftBtnNameId > -1) {
					leftView.setText(leftBtnNameId);
					leftView.setVisibility(View.VISIBLE);
				}
				
				final int leftBtnImageId = bundle.getInt(BaseKeyConstants.KEY_LEFTBTNIMAGE, -1);
				if (leftBtnImageId > -1) {
					leftView.setCompoundDrawablesWithIntrinsicBounds(0, 0, leftBtnImageId, 0);
					leftView.setVisibility(View.VISIBLE);
				}
				
				final int rightBtnNameId = bundle.getInt(BaseKeyConstants.KEY_RIGHTBTNNAME, -1);
				if (rightBtnNameId > -1) {
					rb_right.setText(rightBtnNameId);
					rb_right.setVisibility(View.VISIBLE);
				}
				
				final int rightBtnImageId = bundle.getInt(BaseKeyConstants.KEY_RIGHTBTNIMAGE, -1);
				if (rightBtnImageId > -1) {
					rb_right.setCompoundDrawablesWithIntrinsicBounds(0, 0, rightBtnImageId, 0);
					rb_right.setVisibility(View.VISIBLE);
				}
			}
		}
		
		return mainView;
	}
	
	/**
	 * 设置标题
	 * @param name
	 */
	public void setTitleName(String name) {
		titleName.setText(name);
	}
	
	public void setTitleName(int resId) {
		titleName.setText(resId);
	}
	
	public void setTitleNameStyle(int resid){
		titleName.setTextAppearance(getActivity(), resid);
	}

	public void setTitleNameColor(int color){
		titleName.setTextColor(color);
	}
	
	public void setTitleName(String name, int iconResIds, IActionBarTitleBtnOnClickListener titleBtnOnClickListener) {
		titleName.setText(name);
		this.titleBtnOnClickListener = titleBtnOnClickListener;
		if (titleBtnOnClickListener != null) {
			titleName.setCompoundDrawablesWithIntrinsicBounds(0, 0, iconResIds, 0);
		}
	}
	
	/**
	 * 设置右边按钮
	 * @param resid
	 */
	public void setRightBtnStyle(int resid){
		rb_right.setTextAppearance(getActivity(), resid);
	}
	public void setRightBtnMarginRight(int marginRight){
		RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.MATCH_PARENT );
		params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
		params.addRule(RelativeLayout.CENTER_IN_PARENT);
		params.setMargins(0, 0, marginRight, 0);
		rb_right.setLayoutParams(params);
	}
	public void setRightBtnName(String name) {
		rb_right.setText(name);
	}
	public void setRightBtnName(int resId) {
		rb_right.setText(resId);
	}
	public void setRightBtnName(String name, IActionBarRightBtnOnClickListener rightBtnOnClickListener) {
		rb_right.setText(name);
		if (name.equals("")) {
			rb_right.setVisibility(View.GONE);
		}else{
			rb_right.setVisibility(View.VISIBLE);
		}
		this.rightBtnOnClickListener = rightBtnOnClickListener;
	}

	public void setRightBtnName(String name, int iconIds, IActionBarRightBtnOnClickListener rightBtnOnClickListener) {
		rb_right.setText(name);
		if (iconIds > 0) {
			rb_right.setCompoundDrawablesWithIntrinsicBounds(0, 0, iconIds, 0);
		}
		if (rb_right.getVisibility() != View.VISIBLE) {
			rb_right.setVisibility(View.VISIBLE);
		}
		this.rightBtnOnClickListener = rightBtnOnClickListener;
	}

	public void setRightBtnName(int resId, IActionBarRightBtnOnClickListener rightBtnOnClickListener) {
		rb_right.setText(resId);
		if (rb_right.getText().equals("")) {
			rb_right.setVisibility(View.GONE);
		}else{
			rb_right.setVisibility(View.VISIBLE);
		}
		this.rightBtnOnClickListener = rightBtnOnClickListener;
	}

	public void setRightBtnOnClickListener(IActionBarRightBtnOnClickListener rightBtnOnClickListener) {
		this.rightBtnOnClickListener = rightBtnOnClickListener;
	}

	/**
	 * 设置右边按钮，图片
	 * @param resId
	 */
	public void setRightBtnImage(int resId, IActionBarRightBtnOnClickListener rightBtnOnClickListener) {
		rb_right.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
		if (rb_right.getVisibility() != View.VISIBLE) {
			rb_right.setVisibility(View.VISIBLE);
		}
		this.rightBtnOnClickListener = rightBtnOnClickListener;
	}
	/**
	 * 设置右边按钮，图片
	 * @param resId
	 */
	public void setRightImage(int resId) {
		rb_right.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
		if (rb_right.getVisibility() != View.VISIBLE) {
			rb_right.setVisibility(View.VISIBLE);
		}
	}
	public RadioButton getRightButton(){
		return rb_right;
	}
	public TextView getLeftButton(){
		return leftView;
	}
	public void setRightButton(int resId,int text,int color, IActionBarRightBtnOnClickListener rightBtnOnClickListener) {
		rb_right.setBackgroundResource(resId);
		rb_right.setText(text);
		rb_right.setTextColor(color);
		if (rb_right.getVisibility() != View.VISIBLE) {
			rb_right.setVisibility(View.VISIBLE);
		}
		this.rightBtnOnClickListener = rightBtnOnClickListener;
	}

	/**
	 * 设置右侧按钮显示状态
	 *
	 * @param visible
	 */
	public void setRightBtnVisible(int visible) {
		rb_right.setVisibility(visible);
	}

	/**
	 * 获取右侧按钮显示状态
	 *
	 * @return
	 */
	public int getRightBtnVisible() {
		return rb_right.getVisibility();
	}

	/**
	 * 设置左边按钮
	 * @param name
	 */
	public void setLeftBtnName(String name, IActionBarLeftOnClickListener leftOnClickListener) {
		leftView.setText(name);
		if (name.equals("")) {
			leftView.setVisibility(View.GONE);
		}else{
			leftView.setVisibility(View.VISIBLE);
		}
		this.leftOnClickListener = leftOnClickListener;
	}


	public void setLeftBtnName(int resId, IActionBarLeftOnClickListener leftOnClickListener) {
		leftView.setText(resId);
		if (leftView.getVisibility() != View.VISIBLE) {
			leftView.setVisibility(View.VISIBLE);
		}
		this.leftOnClickListener = leftOnClickListener;
	}

	public void setLeftBtnStyle(int resid){
		leftView.setTextAppearance(getActivity(), resid);
	}

	public void setLeftBtnColor(int color){
		leftView.setTextColor(color);
	}

	public void setLeftBtnMarginRight(int marginLeft){

		RelativeLayout.LayoutParams params = (LayoutParams) leftView.getLayoutParams();
		if(params == null){
			params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT );
		} 
		params.leftMargin = marginLeft ;
		leftView.setLayoutParams(params); 
	}
	
	
	public void setLeftButton(int resId, IActionBarLeftOnClickListener leftOnClickListener) {
		leftView.setCompoundDrawablesWithIntrinsicBounds(resId, 0, 0, 0);
		if (leftView.getVisibility() != View.VISIBLE) {
			leftView.setVisibility(View.VISIBLE);
		}
		this.leftOnClickListener = leftOnClickListener;
	}

	public void setLeftButtonOnRight(int resId, String text, IActionBarLeftOnClickListener leftOnClickListener) {
		leftView.setText(text);
		leftView.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
		if (text.equals("")) {
			leftView.setVisibility(View.GONE);
		}else{
			leftView.setVisibility(View.VISIBLE);
		}
		this.leftOnClickListener = leftOnClickListener;
	}
	/**
	 * 设置左边按钮，图片
	 * @param resId
	 */
	public void setLeftBtnImage(int resId, IActionBarLeftOnClickListener leftOnClickListener) {
		leftView.setCompoundDrawablesWithIntrinsicBounds(0, 0, resId, 0);
		if (leftView.getVisibility() != View.VISIBLE) {
			leftView.setVisibility(View.VISIBLE);
		}
		this.leftOnClickListener = leftOnClickListener;
	}
	/**
	 * 设置背景色
	 * @param color
	 */
	public void setBackgroundColor(int color){
		titleLayout.setBackgroundColor(color);
	}
	/**
	 * 设置左边按钮隐藏
	 */
	public void setLeftBtnVisible(){
		leftView.setVisibility(View.GONE);
	}
	
	public void setTitleBackground(int resid){
		titleLayout.setBackgroundResource(resid);
	}
	
	public void setLeftBtnOnClickListener(IActionBarLeftOnClickListener leftOnClickListener) {
		this.leftOnClickListener = leftOnClickListener;
	}
	
	
	public interface IActionBarLeftOnClickListener {
		public void onClick(View view);
	}
	
	public interface IActionBarLeft2OnClickListener {
	    public void onClick(View view);
	}
	
	public interface IActionBarRightBtnOnClickListener {
		public void onClick(View view);
	}
	
	public interface IActionBarTitleBtnOnClickListener {
		public void onClick(View view);
	}
	
}
